// 运行时配置
import { RunTimeLayoutConfig } from '@umijs/max';
import { message } from 'antd';
import type { RequestConfig } from 'umi';
import { getAccountInfo, getAccountResources } from './services/account/AccountController';
import "./index.less"

// 全局初始化数据配置，用于 Layout 用户信息和权限初始化
// 更多信息见文档：https://umijs.org/docs/api/runtime-config#getinitialstate
export async function getInitialState() {
  let data = await getAccountInfo()
  let resources = await getAccountResources()
  return { name: data.nickname, resources, account: data };
}

export const request: RequestConfig = {
  timeout: 1000,
  // other axios options you want
  errorConfig: {
    errorHandler(error: any) {
      if (error?.response?.status === 401)
        window.location.href = '/auth/login?next=' + window.location.pathname;
      else if (error?.response?.status === 403)
        message.error("权限不足")
      else if (error?.response?.status === 400)
        message.error("参数错误：" + error?.response?.data?.message)
      else if (error?.response?.status === 409)
        message.error(error?.response?.data)
      else if (error?.response?.status >= 500)
        message.error("服务错误")
    },
    errorThrower() {
      console.log('errorThrower');
    },
  },
  requestInterceptors: [],
  responseInterceptors: [],
};

export const layout: RunTimeLayoutConfig = (initialState) => {
  return {
    title: "Ane Admin",
    logo: '/icon_inverted.svg',
    // menu: {
    //   locale: false,
    //   loading: initialState.loading,
    // },
    logout: () => { window.location.href = "/auth/logout" },
    // onPageChange: (location) => { console.log("切换到：" + location.pathname) },
    // footerRender: () => <Footer />,
    // headerRender: () => <Footer />,
    // headerRender: () => <Footer />,
    // rightRender: false,
    // childrenRender:()=><Footer />,
    // rightRender: rightContent,
    // menuHeaderRender: (logo, title) => <>{title}</>,
    // onMenuHeaderClick: () => console.log(33),
    splitMenus: true,
    layout: "mix",
    // menuFooterRender: () => <a href="https://pro.ant.design">pro.ant.design</a>,
    // appList: [{icon:"d",title:"sd"}],
    // links: [<a href="ant.design"> 访问官网 </a>, <a href="help.ant.design"> 帮助 </a>],
  }
};